
package com.scs.application.modules.cert.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.cert.entity.CertificateRepository;
import com.scs.application.modules.cert.entity.CertificateRepositoryHistory;
import com.scs.application.modules.cert.mapper.CertificateRepositoryHistoryMapper;
import com.scs.application.modules.cert.mapper.CertificateRepositoryMapper;
import com.scs.application.modules.cert.request.CertificateRepositoryHistoryQueryRequest;
import com.scs.application.modules.cert.service.CertificateRepositoryHistoryService;
import com.scs.application.modules.design.service.QueryGenerator;
import com.scs.application.modules.ext.entity.CertHosup;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 资质历史版本 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/cert/certificate_repository_historys")
public class CertificateRepositoryHistoryController extends CrudController<CertificateRepositoryHistory, String, CertificateRepositoryHistoryService> implements ImAndExAbleController<CertificateRepositoryHistoryQueryRequest> {
    private CertificateRepositoryHistoryMapper certificateRepositoryHistoryMapper;

    @GetMapping("/page")
    public PageResult page(CertificateRepositoryHistoryQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(CertificateRepositoryHistoryQueryRequest request) {
        List<CertificateRepositoryHistory> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/history")
    public PageResult history(CertificateRepositoryHistoryQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    /***
     * @param request
     * @date: 2024/6/21
     * @Description:  资质预警分页查询
     */
    @GetMapping("/historyPageForWarn")
    public PageResult historyPageForWarn(HttpServletRequest request) {
        IPage<CertificateRepositoryHistory> page = new Page();
        QueryWrapper<CertificateRepository> queryWrapper = QueryGenerator.initQueryWrapper(request);
        if (!UserUtils.isAdmin()) {
            // 只筛选有供货关系的医院
            if (UserUtils.isSupplier()) {
                queryWrapper.eq("supplier_id", UserUtils.getSupplierId());
            }
            // 只筛选当前医院
            if (UserUtils.isHospital()) {
//                queryWrapper.eq("hospital_id", UserUtils.getHospitalId());
            }
        }
        IPage pageResult = certificateRepositoryHistoryMapper.pageWarn(page, queryWrapper, null, null);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }



}
